﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 03/01/2009
// Time: 10:36
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of frmRegisterEmployee.
	/// </summary>
	public partial class frmRegisterEmployee : frmRegisterPerson
	{
		private Employee employee = new Employee();
		private Employees listEmployee = new Employees();
		
		public frmRegisterEmployee()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			this.dgvMain.Size = new Size(808, 426);
			
			base.person = employee.person;
		}
		
		#region overrided methods
			protected override void newEvent()
			{
				employee.clear();
				base.newEvent();
			}
			
			protected override bool delete()
			{
				return base.delete();
			}
			
			protected override void fillDataGridView()
			{
				//base.fillDataGridView();
				listEmployee.fillList();
				
				int tam = listEmployee.count();
				
				dgvMain.Rows.Clear();
				for (int i=0; i < tam; i++)
					dgvMain.Rows.Add ( new String[] {listEmployee.get(i).id,
					                  					listEmployee.get(i).person.id,
					                  					listEmployee.get(i).person.code,
					                  				   listEmployee.get(i).person.name, 
					                  				   listEmployee.get(i).person.cpfCnpj
					                  				   });
			}
			
			protected override void find()
			{
				base.find();
			}
			
			protected override void init()
			{
				DataGridViewTextBoxColumn idEmployee = new DataGridViewTextBoxColumn();
				
				idEmployee.Visible = false;
				idEmployee.Name    = "employeeId";
				dgvMain.Columns.Add(idEmployee);
				
				base.init();
			}
			
			protected override bool save()
			{
				//Save person
				common.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
				if (!base.save())
				{
					common.Database.RollBack();
					return false;
				}
				if (!employee.save())
				{
					common.Database.RollBack();
					return false;
				}
				common.Database.Commit();
				return true;
			}
			
			protected override void setClass()
			{
				base.setClass();
				employee.assist = chkAssist.Checked;
			}
			
			protected override void setEdits()
			{
				//base.setEdits();
				employee.id = dgvMain.CurrentRow.Cells["employeeId"].Value.ToString();
				
				if (employee.get())
				{
					base.fillEdits();
					fillEditsEmployee();
				}
			}
			
			protected override bool update()
			{
				if (!base.update())
					return false;
				return employee.update();
			}
			
		#endregion
		
		protected void fillEditsEmployee()
		{
			chkAssist.Checked = employee.assist;
		}
	}
}
